package cn.iocoder.yudao.module.asset.dal.mysql.manager;

import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.MPJLambdaWrapperX;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.asset.dal.dataobject.manager.ManagerDO;
import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptDO;
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.asset.controller.admin.manager.vo.*;

/**
 * 资产管理 Mapper
 *
 * @author 超级管理员
 */
@Mapper
public interface ManagerMapper extends BaseMapperX<ManagerDO> {

    default PageResult<ManagerRespVO> selectPage(ManagerPageReqVO reqVO) {
        MPJLambdaWrapperX<ManagerDO> query = new MPJLambdaWrapperX<ManagerDO>()
                .likeIfPresent(ManagerDO::getAssetNumber, reqVO.getAssetNumber())
                .likeIfPresent(ManagerDO::getBarcodeNumber, reqVO.getBarcodeNumber())
                .likeIfPresent(ManagerDO::getTitle, reqVO.getTitle())
                .likeIfPresent(ManagerDO::getStorageLocation, reqVO.getStorageLocation())
                .eqIfPresent(ManagerDO::getAssetType, reqVO.getAssetType())
                .likeIfPresent(ManagerDO::getBrand, reqVO.getBrand())
                .eqIfPresent(ManagerDO::getSpecificationModel, reqVO.getSpecificationModel())
                .eqIfPresent(ManagerDO::getNumber, reqVO.getNumber())
                .eqIfPresent(ManagerDO::getUnitOfMeasurement, reqVO.getUnitOfMeasurement())
                .eqIfPresent(ManagerDO::getOriginalValue, reqVO.getOriginalValue())
                .betweenIfPresent(ManagerDO::getObtainTime, reqVO.getObtainTime())
                .eqIfPresent(ManagerDO::getManagementDept, reqVO.getManagementDept())
                .eqIfPresent(ManagerDO::getManagementUser, reqVO.getManagementUser())
                .eqIfPresent(ManagerDO::getUseDept, reqVO.getUseDept())
                .eqIfPresent(ManagerDO::getUseUser, reqVO.getUseUser())
                .eqIfPresent(ManagerDO::getBusinessStatus, reqVO.getBusinessStatus())
                .eqIfPresent(ManagerDO::getNewnessRate, reqVO.getNewnessRate())
                .betweenIfPresent(ManagerDO::getFinancialAccountingTime, reqVO.getFinancialAccountingTime())
                .eqIfPresent(ManagerDO::getRemark, reqVO.getRemark())
                .betweenIfPresent(ManagerDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(ManagerDO::getId);
        query.leftJoin(AdminUserDO.class, "admin1", AdminUserDO::getId, ManagerDO::getUseUser)
                .selectAs("admin1", AdminUserDO::getNickname, ManagerRespVO::getUseUserName)
                .leftJoin(DeptDO.class, "dept1", DeptDO::getId, "admin1", AdminUserDO::getDeptId)
                .selectAs("dept1", DeptDO::getName, ManagerRespVO::getUseDeptName)
                .leftJoin(AdminUserDO.class, "admin2", AdminUserDO::getId, ManagerDO::getManagementUser)
                .selectAs("admin2", AdminUserDO::getNickname, ManagerRespVO::getManagementUserName)
                .leftJoin(DeptDO.class, "dept2", DeptDO::getId, "admin2", AdminUserDO::getDeptId)
                .selectAs("dept2", DeptDO::getName, ManagerRespVO::getManagementDeptName)
                .selectAll(ManagerDO.class);

        return selectJoinPage(reqVO, ManagerRespVO.class, query);
    }

    @TenantIgnore
    default ManagerRespVO selectOne(ManagerPageReqVO reqVO) {
        MPJLambdaWrapperX<ManagerDO> query = new MPJLambdaWrapperX<ManagerDO>()
                .eqIfPresent(ManagerDO::getBarcodeNumber, reqVO.getBarcodeNumber());
        query.leftJoin(AdminUserDO.class, "admin1", AdminUserDO::getId, ManagerDO::getUseUser)
                .selectAs("admin1", AdminUserDO::getNickname, ManagerRespVO::getUseUserName)
                .leftJoin(DeptDO.class, "dept1", DeptDO::getId, "admin1", AdminUserDO::getDeptId)
                .selectAs("dept1", DeptDO::getName, ManagerRespVO::getUseDeptName)
                .leftJoin(AdminUserDO.class, "admin2", AdminUserDO::getId, ManagerDO::getManagementUser)
                .selectAs("admin2", AdminUserDO::getNickname, ManagerRespVO::getManagementUserName)
                .leftJoin(DeptDO.class, "dept2", DeptDO::getId, "admin2", AdminUserDO::getDeptId)
                .selectAs("dept2", DeptDO::getName, ManagerRespVO::getManagementDeptName)
                .selectAll(ManagerDO.class).getSqlSelect();

        return selectJoinOne(ManagerRespVO.class, query);
    }

}
